<template>
  <div>
    <el-button type="primary" class="mb-4" @click="addLanguage">新增</el-button>
    <el-table :data="tableData" style="height: calc(100vh - 250px)">
      <el-table-column prop="id" label="ID" width="100"/>
      <el-table-column prop="name" label="站点类型" align="center" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <el-switch
            :width="58"
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            active-text="开启"
            inactive-text="关闭"
            inline-prompt
            @change="switchType(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="description" label="描述" >
      <template #default="scope">
          <el-tooltip effect="dark" placement="top-start">
            <template #content>
               <div style="max-width: 500px;">
                {{ scope.row.description }}
              </div>
            </template>
            <el-text line-clamp="2">
              {{ scope.row.description }}
            </el-text>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="handleEdit(scope.row)">
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      :limit="searchFrom.pageSize"
      :page="searchFrom.pageNum"
      @pagination="handleCurrentChange"
    ></Pagination>

    <el-dialog
      v-model="dialogVisible"
      :title="title"
      :before-close="handleClose"
      width="650"
    >
      <el-form
        :model="languageParams"
        ref="siteTypeForm"
        label-position="left"
        :rules="languageRules"
      >
        <el-form-item label="站点类型" prop="name" required>
          <el-input
            v-model.trim="languageParams.name"
            placeholder="请输入"
            maxlength="20"
          />
        </el-form-item>
        <el-form-item label="类型状态" prop="status" required>
          <el-switch
            :width="58"
            v-model="languageParams.status"
            :active-value="1"
            :inactive-value="0"
            inline-prompt
            active-text="开启"
            inactive-text="关闭"
          />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            show-word-limit
            type="textarea"
            maxlength="300"
            v-model.trim="languageParams.description"
            placeholder="请输入"
            rows="8"
            resize="none"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="handleSure(siteTypeForm)">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
  <script setup >
import { ref, reactive, onMounted } from "vue";
const { proxy } = getCurrentInstance();
const publicItem = ref({});
const dialogVisible = ref(false); //默认不显示弹出框
const tableData = ref([{}]);
const title = ref("新增");
const isAdd = ref(true); //默认是新增
const siteTypeForm = ref();
const switchLoading = ref(false);
const total = ref(0);
const languageParams = ref({
  name: "",
  status: 1,
  description: "",
});
const searchFrom = ref({
  pageNum: 1,
  pageSize: 10,
}); //搜索参数
const languageRules = reactive({
  name: [
    {
      required: true,
      message: "请输入",
      trigger: "blur",
    },
  ],
});

onMounted(() => {
  getList();
});
// 获取列表
const getList = () => {
  site.general
    .sitewebsitetypeList({
      data: searchFrom.value,
    })
    .then((res) => {
      if(res.code === 200) {
      switchLoading.value=true
    }

      total.value = res.total;
      tableData.value = res.data;
    });
};
// 编辑
const handleEdit = (rowItem) => {
  publicItem.value = rowItem;
  dialogVisible.value = true;
  isAdd.value = false;
  title.value = "编辑";

  languageParams.value.name = rowItem.name;
  languageParams.value.description = rowItem.description;
  languageParams.value.status = rowItem.status;
};
const addLanguage = () => {
  publicItem.value = {};
  dialogVisible.value = true;
  title.value = "新增";
  isAdd.value = true;
};
// 取消
const handleClose = () => {
  proxy.resetForm("siteTypeForm");
  isAdd.value = false;
  dialogVisible.value = false;
  languageParams.value.name=''
  languageParams.value.status=1
  languageParams.value.description=''


};
// 确定
const handleSure = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid, fields) => {
    if (valid) {
      if (isAdd.value) {
        site.general
          .sitewebsitetypeAdd({
            data: languageParams.value,
          })
          .then((res) => {
            if (res.code === 200) {
              handleClose();
              getList();
              proxy.$modal.msgSuccess("新增成功");
            }
          });
      } else {
        site.general
          .sitewebsitetypeUpdate({
            pathParams: {
              id: publicItem.value.id,
            },
            data: languageParams.value,
          })

          .then((res) => {
            if (res.code === 200) {
              handleClose();
              getList();
              proxy.$modal.msgSuccess("修改成功");
            }
          });
      }
    }
  });
};
// 删除
const handleDelete = (rowItem) => {
  var newArray = [];
  newArray.push(rowItem.id);
  proxy.$modal.confirm("是否确认删除此数据类型？").then(() => {
    site.general
      .sitewebsitetypeDeletebatch({
        data: {
          ids: newArray,
        },
      })
      .then((res) => {
        if (res.code === 200) {
          getList();
          proxy.$modal.msgSuccess("删除成功");
        }
      });
  });
};
const switchType = (rowItem,row) => {
  if(!switchLoading.value) return
  console.log(rowItem);
  proxy.$modal
    .confirm("是否确认修改此数据类型？")
    .then(() => {
      site.general
        .sitewebsitetypeUpdatestatus({
          pathParams: {
            id: rowItem.id,
          },
          params: {
            status: rowItem.status,
          },
        })
        .then((res) => {
          if (res.code === 200) {
            getList();
            proxy.$modal.msgSuccess(res.message);
          }
        }).catch(err=>{
          if (rowItem.status) {
        return (rowItem.status = 0);
      } else {
        return (rowItem.status = 1);
      }
        })
    })
    .catch((err) => {
      if (rowItem.status) {
        return (rowItem.status = 0);
      } else {
        return (rowItem.status = 1);
      }
    });
};
// 分页
const handleCurrentChange = (newPage) => {
  searchFrom.value.pageNum = newPage.page;
  searchFrom.value.pageSize = newPage.limit;
  getList();
};
</script>